package com.smarthome.web.property.controller.account;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.account.AccountDoorService;
import com.smarthome.domain.bean.ShAccountDoor;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.enums.AccountDoorStatus;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.web.property.utils.UserHolder;

@Controller
@RequestMapping(value="/accountDoor")
public class AccountDoorController {
	
	@Resource
	private AccountDoorService accountDoorService;
	
	@RequestMapping("/views")
	public String views(HttpServletRequest request){
		boolean isAdmin = UserHolder.isSuperResidence();
		request.setAttribute("isAdmin", isAdmin);
		if(!isAdmin){
			Long residenceId = UserHolder.getCurrentResidenceId();
			request.setAttribute("residenceId", residenceId);
		}
		return "/account/accountHouseCheck";
	}
	
	@RequestMapping("/dataGrid")
	@ResponseBody
	public DataGrid dataGrid(ShAccountDoor accountDoor, PageHelper page,Long accountId){
		accountDoor.setAccountId(accountId);
		return accountDoorService.queryDataGrid(accountDoor, page);
	} 
	
	@RequestMapping("/add")
	@ResponseBody
	public Result add(ShAccountDoor accountDoor){
		Result res=null;
		int owner=accountDoor.getOwner();
		if(accountDoorService.accountExist(accountDoor)){
			res=Result.failure("该门牌下用户已经存在！");
		}else if(owner==1&&accountDoorService.ownerExist(accountDoor.getDoorId())){
			res=Result.failure("该门牌下业主已经存在！");
		}else{
			accountDoor.setOwner(owner);
			accountDoor.setStatus(AccountDoorStatus.OK.getType());
			accountDoorService.add(accountDoor);
			res=Result.success("添加成功！");
		}
		return res;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Result delete(ShAccountDoor accountDoor){
		accountDoorService.delete(accountDoor);
		return Result.success("删除成功！");
	}
	
	@RequestMapping("/checkOk")
	@ResponseBody
	public Result checkOk(Long id){
		ShAccountDoor accountDoor = accountDoorService.queryDataById(id);
		accountDoor.setStatus(1);
		accountDoorService.hasOtherHouse(accountDoor);
		accountDoorService.add(accountDoor);
		return Result.success("操作成功！");
	}
}
